﻿-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Wolf Lai
-- Create date: 2013.07.09
-- Description:	添加采购单记录
-- =============================================
CREATE PROCEDURE [dbo].[MP_AddPurchasesBill] 
	-- Add the parameters for the stored procedure here
	@Bill		dbo.M7_PurchasesBill READONLY,
	@Items		dbo.M7_PurchasesBillItem READONLY
AS DECLARE
	@DrugId		bigint,
	@BatchNo	nvarchar(64),
	@LotNo		int,
	@UnitId		int,
	@Spec		decimal(18,4),
	@Count		int,
	@ErrorCode	int
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	BEGIN TRANSACTION
    -- 1. Add PurchasesBill
    INSERT INTO M7_PurchasesBill
			(BillId,		BillDate,	RecordTime,	VendorId
			,OperatorId,	CheckerId,	BuyerId,	BookKeeperId
			,StoreKeeperId,	BillAmount,	PayAmount,	Remarks
			,DeptId,		PaymentType)
		SELECT
			bill.BillId,		bill.BillDate,		bill.RecordTime,	bill.VendorId,
			bill.OperatorId,	bill.CheckerId,		bill.BuyerId,		bill.BookKeeperId,
			bill.StoreKeeperId,	bill.BillAmount,	bill.PayAmount,		bill.Remarks,
			bill.DeptId,		bill.PaymentType
		FROM @Bill AS bill
    
    -- 2. Add PurchasesBillItem
    INSERT INTO M7_PurchasesBillItem
			(BillId,	DrugId,			StockSpec,		StockUnitId
			,Number,	PurchasesPrice,	SalesPrice,		BatchNumber
			,LotNumber,	ProducedDate,	AvailableDate,	PackUnitId)
		SELECT
			item.BillId,	item.DrugId,			item.StockSpec,		item.StockUnitId,
			item.Number,	item.PurchasesPrice,	item.SalesPrice,	item.BatchNumber,
			item.LotNumber,	item.ProducedDate,		item.AvailableDate,	item.PackUnitId
		FROM @Items item
    
    -- 3. Add ChargeRecord
    INSERT INTO M7_ChargeRecord
			(DeptId,	RecordTime,		Amount,		BillId
			,BillType,	PaymentType,	IsValid,	Remarks)
		SELECT
			bill.DeptId,	bill.RecordTime,	bill.PayAmount,	bill.BillId,
			1,				bill.PaymentType,	1,				NULL
		FROM @Bill AS bill
    
    -- 4. Update Stock
    -- 4.0 Debug
    INSERT INTO M7_DrugStock
			(DrugId,		BatchNumber,	LotNumber,		StockUnitId
			,StockNumber,	ProducedDate,	AvailableDate,	IsOverdue)
		SELECT
			item.DrugId,		item.BatchNumber,	item.LotNumber,		item.StockUnitId,
			item.StockNumber,	item.ProducedDate,	item.AvailableDate,	item.IsOverdue
		FROM @Items AS item
    -- 4.1 Get stock record count
    -- SELECT @Count = COUNT(*) FROM M7_DrugStock WHERE DrugId = @DrugId
    -- 4.2 record is exist
    -- 4.3 record is NOT exist	
	
	-- 5. Commit and Returen
	COMMIT TRANSACTION
	
	SET @ErrorCode = 1
	
	RETURN @ErrorCode
END
GO
